package com.example.charper04.entity.service;

import com.example.charper04.entity.Comment;
import com.example.charper04.entity.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CommentService {
    @Autowired
    CommentRepository commentRepository;

    public void SelectCommentById(Integer id){
        Optional<Comment> comment =  commentRepository.findById(id);
        if (comment.isPresent())
            System.out.println(comment.get()                                                                                                                            );
        else
            System.out.println("没找到");
    }

    public List<Comment> SelectAllSortByAId(){
        return
        commentRepository.findAll(Sort.by(Sort.Direction.DESC,"aId"));
    }

    public List<Comment> SelectAllPage(int page,int size){
        Pageable pageable = PageRequest.of(page, size);
        return commentRepository.findAll(pageable).getContent();
    }

    public void addComment(String content,String author,Integer aId){
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setAuthor(author);
        comment.setAId(aId);
        commentRepository.save(comment);
    }
}
